package com.sq.partner.manager.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.dao.IGatewayAgentInfoMtDao;
import com.sq.partner.manager.dao.RuleAgentConfigDao;
import com.sq.partner.manager.mybatis.model.RuleAgent;
import com.sq.partner.manager.service.RuleAgentConfigService;
import com.sq.partner.manager.util.DateUtil;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
@Service
public class RuleAgentConfigServiceImpl implements RuleAgentConfigService {
	private static Logger logger = LogUtil.getLog(RuleAgentConfigServiceImpl.class);	
	@Resource
	private RuleAgentConfigDao ruleAgentDao;
	@Resource
	private IGatewayAgentInfoMtDao agentInfoDao;
	@Override
	public Page<Map<String, Object>> queryRuleAgentPageByCriteria(
			Page<Map<String, Object>> page) throws Exception {
		page = ruleAgentDao.queryRuleAgentPageByCriteria(page);
		return page;
	}
	@Override
	public Page<Map<String, Object>> queryRulePageByCriteria(
			Page<Map<String, Object>> page) throws Exception {
		page = ruleAgentDao.queryRulePageByCriteria(page);
		return page;
	}
	@Override
	public Result delRuleAgentById(String ruleAengntId) throws Exception {
		boolean flag = ruleAgentDao.deleRuleAgentById(ruleAengntId);
		if(flag){
			return	Result.success("删除成功");
		}else{
			return Result.error("删除失败");
		}
	}
	@Override
	public Result saveRuleAgentById(RuleAgent ruleAgent) throws Exception {
		String[] ids = ruleAgent.getRuleAgentIds();
		boolean flag=true;
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMDDHHmmss");
		for (int i = 0; i < ids.length; i++) {
			ruleAgent.setAgentId(ids[i]);
			ruleAgent.setSign(ruleAgent.getRuleId().replace("ULE-", "")+ruleAgent.getAgentId()+df.format(new Date()));
			flag = ruleAgentDao.addRuleAgentById(ruleAgent);
		}
		if(flag){
			return	Result.success("添加成功");
		}else{
			return Result.error("添加失败");
		}
	}
	@Override
	public Result updateRuleAgent(RuleAgent ruleAgent) throws Exception {
		boolean	flag = ruleAgentDao.updateRuleAgent(ruleAgent);
		if(flag){
			return	Result.success("更新成功");
		}else{
			return Result.error("更新失败");
		}
	}
	@Override
	public Page<Map<String, Object>> queryAgentInfoByPlan(
			Page<Map<String, Object>> page) throws Exception {
		page = ruleAgentDao.queryAgentInfoPageByPlanNo(page);
		return page;
	}
	@Override
	public Map<String, Object> queryAgentInfoById(String ruleAgentId) throws Exception {
		Map<String, Object> RuleAgentMap = ruleAgentDao.queryRuleAgentById(ruleAgentId);
		return RuleAgentMap;
	}

}
